package UILayer;
import ControlLayer.*;
import java.util.Scanner;

/**
 * Login class for the program.
 * 
 * @author Group1
 * @version 2011-06-05
 */
public class LoginUI
{
    private Scanner reader;
    private MainUI mainUI;
    private String loginName;
    private String password;
    private LoginController loginController;
    
    /**
     * Creates the loginMenu.
     */
    public LoginUI()
    {
        reader = new Scanner(System.in);
        loginController = new LoginController();
        start();
        loginName = "";
        password = "";
    }
    
    /**
     * Loops until the user types correct loginname and password.
     */
    private void start()
    {
        boolean stop = false;
        System.out.println("Exit the program by enter 'exit' as loginname");
        System.out.println("");
        while(!stop)
        {
            System.out.print("Login name: ");
            loginName = reader.nextLine();
            
            if(loginName.equals("exit"))
            {
                stop = true;
            }
            else
            {
                System.out.print("Password: ");
                password = reader.nextLine();
        
                checkLoginAndPassword(loginName, password);
            }
        }
    }
    
    /**
     * Checks validity on login and password.
     * @param loginName The name which is used to login to the program
     * @param password The password which is used to login to the program
     */
    private void checkLoginAndPassword(String loginName, String password)
    {
        try
        {
            if(loginController.checkLoginNameAndPassword(loginName, password) == true)
            {
                System.out.println("OK");
                mainUI = new MainUI(loginName);
            }
        
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

    public String getLoginName()
    {
        return loginName;
    }

    public String getPassword()
    {
        return password;
    }
}
